package com.gzsxy.esjy.sign.socket;

import com.gzsxy.esjy.sign.async.AsyncWsClient;
import com.gzsxy.esjy.sign.dto.InnerSignDto;
import com.gzsxy.esjy.sign.enums.SignTypeEnum;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author zhangxiaolong
 * @since 2022/7/15
 */
public interface AsyncWsCallBack {

    /**
     * 请求回调
     *
     * @param asyncWsClient 连接实体
     * @param msg           消息
     */
    public void doResponse(AsyncWsClient asyncWsClient, Object msg);

    /**
     * 连接成功
     *
     * @param asyncWsClient 连接对象
     */
    public void connected(AsyncWsClient asyncWsClient);

    /**
     * 连接断开
     *
     * @param asyncWsClient 连接对象
     */
    public void disConnected(AsyncWsClient asyncWsClient);

    /**
     * 连接异常
     *
     * @param asyncWsClient 连接对象
     * @param cause         异常
     */
    public void exception(AsyncWsClient asyncWsClient, Throwable cause);

    /**
     * 心跳检测
     *
     * @param asyncWsClient 连接对象
     * @param event         事件
     */
    public void heartbeat(AsyncWsClient asyncWsClient, IdleStateEvent event);

    /**
     * 组装内部协议
     */
    default InnerSignDto getSignDto(AsyncWsClient asyncWsClient, String msg, Integer code) {
        InnerSignDto innerSignDto = new InnerSignDto();
        innerSignDto.setSignDevType(SignTypeEnum.getType(asyncWsClient.getDevType()).getName());
        innerSignDto.setMsg(msg);
        innerSignDto.setCode(code);
        innerSignDto.setRequestId(asyncWsClient.getRequestId());
        innerSignDto.setTaskId(asyncWsClient.getTaskId());

        return innerSignDto;
    }
}
